/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.utils.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class DataPairFieldDateTime extends DataPairDefault implements FieldBinding,DataPairFieldDate.DataPairSetListener {
    private static SimpleDateFormat datetime_format = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat time_format = new SimpleDateFormat("HHmmss");
    private BindDateTimeInterface field;
    private DataPairFieldDate field_date = null;
    private DataPairFieldTime field_time = null;
    private BindableProperties bindable = null;
    private BindDate bind_date = null;
    private BindTime bind_time = null;
    private JPanel container = new JPanel();
    
    public DataPairFieldDateTime(Binder binder,BindDateTimeInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFieldDateTime(Binder binder,BindDateTimeInterface p_field, boolean editable) {
        super(binder,p_field);
        try {
            bindable = new BindableProperties();
            bind_date = new BindDate("date","date",bindable);
            bind_time = new BindTime("time","time",bindable);
            
            this.field = p_field;
            container.setLayout(new BorderLayout());
            field_date = new DataPairFieldDate(binder,bind_date, editable);
            field_time = new DataPairFieldTime(binder,bind_time, editable);
            field_date.addDataPairSetListener(this);
            setLabel(new JLabel(field.getDescription()));
            container.add(field_date.getComponent(),BorderLayout.NORTH);
            container.add(field_time.getComponent(),BorderLayout.CENTER);
            setComponent(container);
        } catch (Exception exception) {
            Log.critical.out(exception);
        }
    }
    
    public void dateSet() {
        try {
            field_time.intoBindable();
            if (field_time.getBindField().getObject() == null) {
                ((BindTimeInterface)field_time.getBindField()).set(new Date());
                field_time.intoControl();
            }
        } catch (BindException be) {
            Log.debug.out(be);
        }
    }
    
    private Date getDateTime() {
        if (bind_date.get() == null || bind_time.get() == null) {
            return null;
        }
        try {
            return datetime_format.parse(
                    date_format.format(bind_date.get())+
                    time_format.format(bind_time.get())
                    );
        } catch (Exception exception) {
            Log.debug.out(exception);
        }
        return null;
    }
    
    public boolean isChanged() {
        return (field_date.isChanged() || field_time.isChanged());
    }
    
    public void intoControl() {
        bind_date.set(field.get());
        bind_time.set(field.get());
        field_date.intoControl();
        field_time.intoControl();
    }
    
    public void intoBindable() {
        field_date.intoBindable();
        field_time.intoBindable();
        field.set(getDateTime());
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindDateTimeInterface) p_field;
    }
    
    public void resetControl() {
        field_date.resetControl();
        field_time.resetControl();
    }
}

